package org.jeecg.modules.innerApi.zhiYunApi.convert;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.common.util.MathUtil;
import org.jeecg.modules.innerApi.common.requestDto.*;
import org.jeecg.modules.innerApi.zhiYunApi.requestVo.*;
import org.jeecg.modules.xxgx.common.enums.HsbzEnum;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 将开票相关参数转换为新时代智云相关参数
 */
public class ZyRequestParamConvert {

    /**
     * 待开票数据转换
     */
    public static ZySkfpParam invoiceParamConvert(InvoiceParam invoiceParam){
        ZySkfpParam skfp=new ZySkfpParam();
        skfp.setFpqqlsh(invoiceParam.getFpsqlsh());
        skfp.setKpzdbs(invoiceParam.getFwqxx().getJqbh());
        skfp.setFplxdm(invoiceParam.getFplxdm());
        skfp.setKplx(invoiceParam.getKplx());
        skfp.setTspz(invoiceParam.getTspz());
        skfp.setXhdwmc(invoiceParam.getXfmc());
        skfp.setXhdwsbh(invoiceParam.getXfsh());
        skfp.setXhdwdzdh(invoiceParam.getXfdzdh());
        skfp.setXhdwyhzh(invoiceParam.getXfyhzh());
        skfp.setGhdwmc(invoiceParam.getKhmc());
        skfp.setGhdwsbh(invoiceParam.getKhsh());
        skfp.setGhdwdzdh(invoiceParam.getKhdzdh());
        skfp.setGhdwyhzh(invoiceParam.getKhyhzh());
        //skfp.setGfkhyx(invoiceParam.getKhlxyx()); (注释不传，使用本地邮箱推送)
        skfp.setGfkhdh(invoiceParam.getKhlxfs());

        skfp.setQdbz(invoiceParam.getQdbz());
        skfp.setZsfs(invoiceParam.getZsfs());
        skfp.setHjje(invoiceParam.getHjje().toPlainString());
        skfp.setHjse(invoiceParam.getHjse().toPlainString());
        skfp.setJshj(MathUtil.add(invoiceParam.getHjje(),invoiceParam.getHjse()).toPlainString());
        skfp.setKce(invoiceParam.getKce() != null ? invoiceParam.getKce().toPlainString():"");
        skfp.setBz(invoiceParam.getBz());
        skfp.setKpr(StringUtils.isEmpty(invoiceParam.getKpr())?invoiceParam.getKpzd().getKpr():invoiceParam.getKpr());
        skfp.setFhr(invoiceParam.getFhr());
        skfp.setSkr(invoiceParam.getSkr());
        skfp.setTzdbh(invoiceParam.getHzxxbbh());
        skfp.setYfpdm(invoiceParam.getYfpdm());
        skfp.setYfphm(invoiceParam.getYfphm());
        List<ZySkfpmxParam> params=new ArrayList<ZySkfpmxParam>();
        invoiceParam.getFpmxs().forEach(djmx -> {
            ZySkfpmxParam param=new ZySkfpmxParam();
            param.setFphxz(StringUtils.isEmpty(djmx.getFphxz())?"0":djmx.getFphxz());
            param.setSpmc(djmx.getSpmc());
            param.setSpsm(djmx.getSpbm());
            param.setGgxh(djmx.getGgxh());
            param.setDw(djmx.getJldw());
            param.setSpsl(djmx.getSpsl()!=null?djmx.getSpsl().toPlainString():"");
            if(HsbzEnum.Y.getValue().equals(invoiceParam.getHsbz())){
                param.setDj(djmx.getHsdj()!=null?djmx.getHsdj().toPlainString():"");
                param.setJe(djmx.getHsje()!=null?djmx.getHsje().toPlainString():"");
            } else {
                param.setDj(djmx.getBhsdj()!=null?djmx.getBhsdj().toPlainString():"");
                param.setJe(djmx.getBhsje()!=null?djmx.getBhsje().toPlainString():"");
            }
            param.setSl(djmx.getTax()!=null?djmx.getTax().toPlainString():"");
            param.setSe(djmx.getTaxje()!=null?djmx.getTaxje().toPlainString():"");
            param.setHsbz(invoiceParam.getHsbz());
            param.setSpbm(djmx.getSpbm());
            param.setZxbm(djmx.getSpdm());
            param.setYhzcbs(StringUtils.isEmpty(djmx.getYhzcbs())?"0":djmx.getYhzcbs());
            param.setLslbs(djmx.getLslbs());
            param.setZzstsgl(djmx.getZzstsgl());
            params.add(param);
        });
        skfp.setFyxm(params);
        return skfp;
    }

    /**
     * 红字申请数据转换
     */
    public static ZyHzxxbsqParam hzxxbsqParamConvert(HzxxbsqParam param){
        ZyHzxxbsqParam zyHzxxbsqParam=new ZyHzxxbsqParam();
        BeanUtils.copyProperties(param,zyHzxxbsqParam);

        List<ZyHzxxbsqParam.ZyHzxxbsqmxParam> zymxList = new ArrayList<>();
        param.getHzsqmxs().forEach(mx -> {
            ZyHzxxbsqParam.ZyHzxxbsqmxParam zyHzxxbsqmxParam = new ZyHzxxbsqParam.ZyHzxxbsqmxParam();
            BeanUtils.copyProperties(mx, zyHzxxbsqmxParam);
            zymxList.add(zyHzxxbsqmxParam);
        });
        zyHzxxbsqParam.setFyxm(zymxList);

        zyHzxxbsqParam.setJqbh(param.getFwqxx().getJqbh());
        return zyHzxxbsqParam;
    }
    /**
     * 红字申请数据转换
     */
    public static ZyHzxxbtbParam hzxxbtbParamConvert(HzxxbtbParam param){
        ZyHzxxbtbParam zyHzxxbtbParam=new ZyHzxxbtbParam();
        BeanUtils.copyProperties(param,zyHzxxbtbParam);
        //转换日期格式
        zyHzxxbtbParam.setSqrqq(DateUtils.fromStrToStr(param.getSqrqq(),DateUtils.date_sdf.get(),DateUtils.yyyyMMdd.get()));
        zyHzxxbtbParam.setSqrqz(DateUtils.fromStrToStr(param.getSqrqz(),DateUtils.date_sdf.get(),DateUtils.yyyyMMdd.get()));
        zyHzxxbtbParam.setJqbh(param.getFwqxx().getJqbh());
        return zyHzxxbtbParam;
    }
    /**
     * 红字申请撤销数据转换
     */
    public static ZyHzxxbcxParam hzxxbcxParamConvert(HzxxbcxParam param){
        ZyHzxxbcxParam zyHzxxbcxParam=new ZyHzxxbcxParam();
        BeanUtils.copyProperties(param,zyHzxxbcxParam);
        if(StringUtils.isEmpty(param.getSqlsh())) {
            zyHzxxbcxParam.setSqlsh(IdWorker.get32UUID());
        }
        zyHzxxbcxParam.setJqbh(param.getFwqxx().getJqbh());
        zyHzxxbcxParam.setXxbbh(param.getHzxxbbh());
        return zyHzxxbcxParam;
    }
    /**
     * 红字申请下载数据转换
     */
    public static ZyHzxxbxzParam hzxxbxzParamConvert(HzxxbxzParam param){
        ZyHzxxbxzParam zyHzxxbxzParam=new ZyHzxxbxzParam();
        BeanUtils.copyProperties(param,zyHzxxbxzParam);
        zyHzxxbxzParam.setJqbh(param.getFwqxx().getJqbh());
        zyHzxxbxzParam.setXxbbh(param.getHzxxbbh());
        return zyHzxxbxzParam;
    }
    /**
     * 生成PDF数据转换
     */
    public static ZyCreatePdfParam createPdfParamConvert(CreatePdfParam param){
        ZyCreatePdfParam zyCreatePdfParam=new ZyCreatePdfParam();
        BeanUtils.copyProperties(param,zyCreatePdfParam);
        zyCreatePdfParam.setJqbh(param.getFwqxx().getJqbh());
        return zyCreatePdfParam;
    }

    /**
     * 当前未开票发票号数据转换
     */
    public static ZyCurrentFphmParam currentFphmParamConvert(CurrentFphmParam param){
        ZyCurrentFphmParam zyCurrentFphmParam=new ZyCurrentFphmParam();
        BeanUtils.copyProperties(param,zyCurrentFphmParam);
        zyCurrentFphmParam.setJqbh(param.getFwqxx().getJqbh());
        return zyCurrentFphmParam;
    }

    /**
     * 发票作废参数转换
     * @param param
     * @return
     */
    public static ZyFpzfParam fpzfParamConvert(FpzfParam param){
        ZyFpzfParam zyFpzfParam=new ZyFpzfParam();
        BeanUtils.copyProperties(param,zyFpzfParam);
        zyFpzfParam.setJqbh(param.getFwqxx().getJqbh());
        return zyFpzfParam;
    }

    public static ZyInvoiceQueryParam invoiceQueryParamConvert(InvoiceQueryParam param){
        ZyInvoiceQueryParam zyInvoiceQueryParam=new ZyInvoiceQueryParam();
        BeanUtils.copyProperties(param,zyInvoiceQueryParam);
        zyInvoiceQueryParam.setJqbh(param.getFwqxx().getJqbh());
        zyInvoiceQueryParam.setKprqq(DateUtils.formatDate(param.getKprqq(),"yyyyMMdd"));
        zyInvoiceQueryParam.setKprqz(DateUtils.formatDate(param.getKprqz(),"yyyyMMdd"));
        return zyInvoiceQueryParam;
    }

}
